package com.ruibang.glass.equipment.util;


import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @ClassName : DataUtil
 * @Description : 数据处理工具类
 * @Author : wujie
 * @Date: 2020-06-09 21:43:04
 */
@Slf4j
public class DataUtil {

    /***
     * 获取图片字符串
     * @param imagePath
     * @return
     */
    public static String getImageStr(String imagePath) {
        InputStream in = null;
        byte[] data = null;
        try {
            URL url = new URL(imagePath);
            URLConnection conn = url.openConnection();
            in = conn.getInputStream();
            //根据图片实际大小初始化字节数组
            data = new byte[in.available()];
            in.read(data);

        } catch (IOException e) {
            log.error("获取图片信息失败:" + e.getMessage());
            return "";
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //使用base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }


    /***
     * 统计字符串中中文，英文，数字，空格等字符个数
     * @param str 需要统计的字符串
     */
    public static Integer count(String str) {
        int chCharacter = 0;//中文字符
        int enCharacter = 0;//英文字符
        int spaceCharacter = 0;//空格
        int numberCharacter = 0;//数字
        int otherCharacter = 0;//其他字符

        if (null == str || str.equals("")) {
            return 0;
        }

        for (int i = 0; i < str.length(); i++) {
            char tmp = str.charAt(i);
            if ((tmp >= 'A' && tmp <= 'Z') || (tmp >= 'a' && tmp <= 'z')) {
                enCharacter++;
            } else if ((tmp >= '0') && (tmp <= '9')) {
                numberCharacter++;
            } else if (tmp == ' ') {
                spaceCharacter++;
            } else if (isChinese(tmp)) {
                chCharacter++;
            } else {
                otherCharacter++;
            }
        }
        //log.info("字符串:" + str + " "+"中文字符有:" + chCharacter+"英文字符有:" + enCharacter+"数字有:" + numberCharacter+"空格有:" + spaceCharacter+"其他字符有:" + otherCharacter);
        if (chCharacter > 0) {
            return chCharacter * 2 + enCharacter + spaceCharacter + numberCharacter + otherCharacter;
        } else {
            return 0;
        }
    }

    private static boolean isChinese(char ch) {
        //获取此字符的UniCodeBlock
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
        //  GENERAL_PUNCTUATION 判断中文的“号
        //  CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
        //  HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的，号
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }
}
